package ch10_thread.thread;

/**
 * 两个线程交替打印0-100的奇数、偶数
 * 方法1：synchronize
 * 方法2：wait、notify
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DemoOddEven01 {
    private static int count;
    private static final Object clock = new Object();

    public static void main(String[] args) {
        // 创建两个线程
        //    一个线程处理奇数，一个线程处理偶数
        //    synchronize进行通信
        new Thread(() -> {
            while (count < 100) {
                synchronized (clock) {
                    if (count % 2 == 0) {
                        System.out.println("偶数：" + count);
                        count++;
                    }
                }
            }
        }).start();

        new Thread(() -> {
            while (count < 100) {
                synchronized (clock) {
                    if (count % 2 != 0) {
                        System.out.println("奇数：" + count);
                        count++;
                    }
                }
            }
        }).start();

    }
}
